import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { PosModel } from '#/api/system/models';

import { isUndefined } from '@vben/utils';

import { z } from '#/adapter/form';

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      dependencies: {
        triggerFields: ['code'],
        disabled(values) {
          return !isUndefined(values.id);
        },
      },
      fieldName: 'code',
      label: '职位代码',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: '职位名称',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 0,
      },
      fieldName: 'order',
      label: '排序',
      defaultValue: 100,
      rules: z.number().gte(0),
    },
    {
      component: 'Switch',
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        class: 'w-auto',
      },
      fieldName: 'status',
      label: '状态',
      defaultValue: 1,
      rules: 'required',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<PosModel>,
): VxeTableGridOptions<PosModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'name', title: '职位名称', width: 220 },
    { field: 'code', title: '职位编码', width: 160 },
    { field: 'order', title: '排序', width: 80 },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: { name: 'CellTag', attrs: { useStatus: true } },
    },
    { field: 'createdTime', title: '创建时间', minWidth: 150 },
    { field: 'updatedTime', title: '修改时间', minWidth: 150 },
    {
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: '职位',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: ['edit', 'delete'],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
